
import 'dart:io';

import 'package:archive/archive.dart';
import 'package:archive/archive_io.dart';
import 'package:path_provider/path_provider.dart';

import 'log_util.dart';

class FileUtil{
  //获取文件名
  static String getFileName({String filePath:''}){
    if(filePath==null)return'';
    List<String> filePaths = filePath.split('/');
    if(filePaths.length<2){
      return filePaths[0];
    }else{
      return filePaths[filePaths.length-1];
    }
  }

  //获取文件前缀
  static String getFileNamePreSuffix({String fileName:''}){
    if(fileName==null)return'';
    List<String> filePaths = fileName.split('.');
    if(filePaths.length<2){
      return filePaths[0];
    }else{
      return filePaths[0];
    }
  }
  //获取文件后缀
  static String getFileNameSuffix({String filePath:''}){
    if(filePath==null)return'';
    List<String> filePaths = filePath.split('.');
    if(filePaths.length<2){
      return filePaths[0];
    }else{
      return filePaths[filePaths.length-1];
    }
  }
  //获取游戏存储文件夹
  static Future<String> getGameDirectory({String gameDirectoryName}) async{
    Directory _path = await getApplicationDocumentsDirectory();
    String _localPath = _path.path + Platform.pathSeparator +gameDirectoryName;
    return _localPath;
  }
  //获取游戏存储文件夹下的启动页面文件
  static Future<File> getGameLaunchFile({String gameDirectoryName,String launchFileName}) async{
    String launchFilePath = (await getGameDirectory(gameDirectoryName: gameDirectoryName))+ Platform.pathSeparator
        +gameDirectoryName + Platform.pathSeparator+'${launchFileName??'index.html'}';
    return File(launchFilePath);
  }
  //获取存储在APP文件夹下的游戏zip包
  static Future<File> getGameZipFile({String gameDirectoryName,String zipFileName}) async {
    Directory _path = await getApplicationDocumentsDirectory();
    String _localPath = _path.path + Platform.pathSeparator +gameDirectoryName+Platform.pathSeparator +zipFileName;
    final savedFile = File(_localPath);
    return savedFile;
  }
  //解压
  static Future<String> decodeZip({String zipFilePath,String outputDictionryPath}) async{
    // Read the Zip file from disk.
    try{
      // final bytes = File(zipFilePath).readAsBytesSync();
      // // Decode the Zip file
      // final archive = ZipDecoder().decodeBytes(bytes);
      // // Extract the contents of the Zip archive to disk.
      // for (final file in archive) {
      //   final filename = file.name;
      //   if (file.isFile) {
      //     final data = file.content as List<int>;
      //     File(filename)
      //       ..createSync(recursive: true)
      //       ..writeAsBytesSync(data);
      //   } else {
      //     await Directory(filename).create(recursive: true);
      //   }
      // }
      LogUtil.d('downLoadOrOpen-->游戏zip包开始解压');
      Directory _path = await getApplicationDocumentsDirectory();
      String _localPath = outputDictionryPath??(_path.path + Platform.pathSeparator);
      final inputStream = InputFileStream('$zipFilePath');
      final archive = ZipDecoder().decodeBuffer(inputStream);
      extractArchiveToDisk(archive, _localPath);
      // LogUtil.d('downLoadOrOpen-->decodeZip zipFilePath=${zipFilePath}');
      // LogUtil.d('downLoadOrOpen-->decodeZip archive=${archive?.toString()}');
      LogUtil.d('downLoadOrOpen-->游戏zip包解压成功');
      dirList(_localPath);
      return _localPath;
    }catch (e){
      LogUtil.d('downLoadOrOpen-->decodeZip e=${e?.toString()} }');
      LogUtil.d('downLoadOrOpen-->游戏zip包解压失败');
      return '';
    }
  }

  static dirList(String filePath) async {

    Directory documentsDirectory = await getApplicationDocumentsDirectory();

    Stream fileList = Directory(filePath).list();

    await for(FileSystemEntity fileSystemEntity in fileList){

      print('里面的文件为：${fileSystemEntity?.toString()}');
      if(fileSystemEntity is Directory){
        dirList(fileSystemEntity?.path);
      }
    }

  }
}
